// pages/book-detail/book-detail.js
import {
  BookModel
} from '../../models/book.js'

const bookModel = new BookModel()

import {
  LikeModel
} from '../../models/like.js'

const likeModel = new LikeModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    book: null,
    likeStatus: false,
    likeCount: 0,
    posting: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    const id = options.id
    const detail = bookModel.getDetail(id)
    const comments = bookModel.getComments(id)
    const likeStatus = bookModel.getLikeStatus(id)

    //Promise.race 竞争，只要任意一个子promise完成就触发该回调函数,res携带的是竞争成功的子promise的res
    //新的promise是这三个的一个合体 ，等所有子promise完成才触发该回调函数，res是所有子promise/res的合体
    //怎么感觉Promise.all性能上不咋地呢
    Promise.all([detail, comments, likeStatus])
      .then(res => {
        this.setData({
          book: res[0],
          comments: res[1].comments,
          likeStatus: res[2].like_status,
          likeCount: res[2].fav_nums
        })
        wx.hideLoading()
      })

    // detail.then(res => {
    //   console.log(res)
    //   this.setData({
    //     book: res
    //   })
    // })
    // comments.then(res => {
    //   console.log(res)
    //   this.setData({
    //     comments: res.comments
    //   })
    // })
    // likeStatus.then(res => {
    //   console.log(res)
    //   this.setData({
    //     likeStatus: res.like_status,
    //     likeCount: res.fav_nums
    //   })
    // })
  },

  onLike(event) {
    const like_or_cancel = event.detail.behavior
    likeModel.like(like_or_cancel, this.data.book.id, 400)
  },

  onFakePost(event) {
    this.setData({
      posting: true
    })
  },

  onCancel(event) {
    this.setData({
      posting: false
    })
  },

  onPost(event) {
    // ||的另一种用途：选择一个值，在这里用于选择一个评论
    // const comment = event.detail.text || event.detail.value
    const comment = this._checkCommentOfOnPost(event)
    if (!comment) {
      return
    }
    bookModel.postComment(this.data.book.id, comment)
      .then(res => {
        wx.showToast({
          title: '+ 1',
          icon: "none"
        })
        this.data.comments.unshift({
          content: comment,
          nums: 1
        })
        this.setData({
          comments: this.data.comments,
          posting: false
        })
      })
  },
  _checkCommentOfOnPost(event) {
    let comment = ''
    if (event.detail.text) {
      comment = event.detail.text
      return comment
    } else {
      if (event.detail.value) {
        comment = event.detail.value
        if (comment.length > 12) {
          wx.showToast({
            title: '短评最多12个字',
            icon: 'none'
          })
          this.setData({
            posting: false
          })
          return
        }
        return comment
      } else {
        wx.showToast({
          title: '短评内容为空',
          icon: 'none'
        })
        this.setData({
          posting: false
        })
        return
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})